import axios from 'axios'
import { Modal, notification } from 'ant-design-vue'
let BASE_URL = process.env.VUE_APP_URL

export const POST = (uri, params = {}) => {
  return new Promise((resolve, reject) => {
    axios({
      url: BASE_URL + uri,
      method: "POST",
      data: params,
      header: {
        "Content-Type": "application/json; charset=utf-8",
        "Cache-Control": "max-age=3600"
      }
    }).then(res => {
      console.log(res, '')
      if (res.status == 200) {
        if(res.data.errorCode == 'SUCCESS'){
          resolve(res.data)
        }else if(res.data.code == -15){
          notification.error({
            message: '错误',
            description: res.data.errMsg
          })
          sessionStorage.clear()
          window.location.reload()
        }else{
          notification.error({
            message: '错误',
            description: res.data.errMsg || '数据获取错误，请重新尝试'
          })
        }
        
      } else if (res.status == 401) {
        Modal.warning({
          title: '登录失效',
          content: '你的登录已失效，请重新登录',
          onOk() {
            sessionStorage.clear()
            window.location.reload()
          }
        })
      } else {
        notification.error({
          message: '错误',
          description: '请求接口失败,请重新尝试'
        })
      }

    }).catch(err => {
      console.log("请求接口失败=", err)
      notification.error({
        message: '提示',
        description: err.msg || err.message || '请求接口失败！'
      })
      reject(err)
    })
  })
}

export const GET = (uri, params = {}) => {
  return new Promise((resolve, reject) => {
    axios.request({
      url: BASE_URL + uri,
      method: "GET",
      data: params,
      header: {
        "Content-Type": "application/json; charset=utf-8"
      },
      success: (res) => {
        if (res.statusCode == 200 && res.data) {
          resolve(res.data)
        } else {
          notification.error({
            message: '提示',
            description: '请检查网络是否通畅'
          })
        }
      },
      fail: (err) => {
        console.log(err)
        notification.error({
          message: '提示',
          description: err.msg || err.message || '请求接口失败！'
        })
        reject(err)
      }
    })
  })
}
